﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using WeighClient.Model;
using WeighClient.Utils;

namespace WeighClient
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        public static DeviceInfo deviceInfo = null;
        private static object _lock = new object();

        public static HttpServer httpServer = null;
        private static object _httpLock = new object();

        public static Config config = null;
        public static RunTimeVariable runTimeVariable = null;
        public static GlobalVariablesModel globalVariables = null;

        public static DeviceInfo GetDeviceInfo()
        {
            if (deviceInfo == null)
            {
                lock (_lock)
                {
                    if (deviceInfo == null)
                    {
                        deviceInfo = new DeviceInfo();
                    }
                }
            }
            return deviceInfo;
        }

        public static Config GetConfig()
        {
            if (config == null)
            {
                lock (_lock)
                {
                    if (config == null)
                    {
                        config = new Config();
                    }
                }
            }
            return config;
        }

        public static RunTimeVariable GetRunTimeVariable()
        {
            if (runTimeVariable == null)
            {
                lock (_lock)
                {
                    if (runTimeVariable == null)
                    {
                        runTimeVariable = new RunTimeVariable();
                    }
                }
            }
            return runTimeVariable;
        }

        public static HttpServer GetHttpServer()
        {
            if (httpServer == null)
            {
                lock (_httpLock)
                {
                    if (httpServer == null)
                    {
                        int port = int.Parse(App.GetConfig().LocalHttpServerPort);
                        httpServer = new HttpServer(port);
                    }
                }
            }
            return httpServer;
        }

        public static GlobalVariablesModel getGlobalVariables()
        {
            if (globalVariables == null)
            {
                lock (_lock)
                {
                    if (globalVariables == null)
                    {
                        globalVariables = new GlobalVariablesModel();
                    }
                }
            }
            return globalVariables;
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            App.GetConfig().DeviceCode = ConfigurationManager.AppSettings["DeviceCode"];
            App.GetConfig().DeviceName = ConfigurationManager.AppSettings["DeviceName"];
            App.GetConfig().DeviceType = ConfigurationManager.AppSettings["DeviceType"];

            App.GetConfig().IC_Card_PortName = ConfigurationManager.AppSettings["IC_Card_PortName"];

            App.GetConfig().IO_Com_PortName = ConfigurationManager.AppSettings["IO_Com_PortName"];
            App.GetConfig().IO_In_Open_Port = ConfigurationManager.AppSettings["IO_In_Open_Port"];
            App.GetConfig().IO_In_Close_Port = ConfigurationManager.AppSettings["IO_In_Close_Port"];
            App.GetConfig().IO_Out_Open_Port = ConfigurationManager.AppSettings["IO_Out_Open_Port"];
            App.GetConfig().IO_Out_Close_Port = ConfigurationManager.AppSettings["IO_Out_Close_Port"];

            App.GetConfig().IO_In_Input_Port = ConfigurationManager.AppSettings["IO_In_Input_Port"];
            App.GetConfig().IO_Out_Input_Port = ConfigurationManager.AppSettings["IO_Out_Input_Port"];

            App.GetConfig().HttpServerIp = ConfigurationManager.AppSettings["HttpServerIp"];
            App.GetConfig().HttpServerPort = ConfigurationManager.AppSettings["HttpServerPort"];

            App.GetConfig().LocalHttpServerPort = ConfigurationManager.AppSettings["LocalHttpServerPort"];

            App.GetConfig().Mq_HostName = ConfigurationManager.AppSettings["Mq_HostName"];
            App.GetConfig().Mq_UserName = ConfigurationManager.AppSettings["Mq_UserName"];
            App.GetConfig().IpImagePath = ConfigurationManager.AppSettings["IpImagePath"];
            App.GetConfig().Mq_Password = ConfigurationManager.AppSettings["Mq_Password"];

            App.GetConfig().NodeRedUrl = ConfigurationManager.AppSettings["NodeRed_URL"];
            App.GetConfig().IfPrint = ConfigurationManager.AppSettings["IfPrint"];
            App.GetConfig().Printer01 = ConfigurationManager.AppSettings["Printer01"];
            App.GetConfig().Printer02 = ConfigurationManager.AppSettings["Printer02"];

            App.GetConfig().Left_LED_IP = ConfigurationManager.AppSettings["Left_LED_IP"];

            App.GetConfig().Right_LED_IP = ConfigurationManager.AppSettings["Right_LED_IP"];
            App.GetConfig().Remote_Print_QueueName = ConfigurationManager.AppSettings["Remote_Print_QueueName"];
            App.GetConfig().IDCardPort1 = ConfigurationManager.AppSettings["IDCardPort1"];
            App.GetConfig().IDCardPort2 = ConfigurationManager.AppSettings["IDCardPort2"];
            App.GetConfig().IDCardPortBaudRate = ConfigurationManager.AppSettings["IDCardPortBaudRate"];
            App.GetConfig().IDCardNumber = ConfigurationManager.AppSettings["IDCardNumber"];
            App.GetConfig().driverMacCard = ConfigurationManager.AppSettings["driverMacCard"];
            App.GetConfig().IDCard = ConfigurationManager.AppSettings["IDCard"];
            App.GetConfig().VlcIp = ConfigurationManager.AppSettings["VlcIp"];
            App.GetConfig().cardReader = ConfigurationManager.AppSettings["cardReader"];
            App.GetConfig().SJIDCardPort = ConfigurationManager.AppSettings["SJIDCardPort"];

            string _autoWeigh = ConfigurationManager.AppSettings["AutoWeigh"];
            if ("true".Equals(_autoWeigh))
            {
                App.GetConfig().AutoWeigh = true;
                App.GetRunTimeVariable().AutoWeigh = true;
            }
            else
            {
                App.GetConfig().AutoWeigh = false;
                App.GetRunTimeVariable().AutoWeigh = false;
            }

            App.GetConfig().Weight_Comm_PortName = ConfigurationManager.AppSettings["Weight_Comm_PortName"];

            HttpServer server = App.GetHttpServer();
            if (!server.IsListening())
            {
                server.Run();
            }

            LogObject.WriteLog("系统启动完成!");
            this.DispatcherUnhandledException += App_DispatcherUnhandledException;
        }

        public App()
        {
            //DispatcherUnhandledException += App_DispatcherUnhandledException;
        }

        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            LogObject.WriteLog("系统异常------------------" + Environment.NewLine + e.Exception.Message + "--------------------------");
            MessageBox.Show("Error encountered! Please contact support." + Environment.NewLine + e.Exception.Message);
            e.Handled = true;
        }
    }
}